<?php
/**
 * Created by PhpStorm.
 * User: tangchao
 * Date: 19-3-25
 * Time: 下午9:02
 */

namespace App\Lib\Cache;

use App\Model\Video;
use EasySwoole\Core\Component\Cache\Cache;
use EasySwoole\Core\Component\Di;
use EasySwoole\Core\Component\Logger;

/**
 * 生成视频列表数据缓存
 * Class VideoList
 * @package App\Lib\Cache
 */
class VideoList
{
    public $type = 'file';
    public $size = 1000;

    /**
     * 生成缓存数据
     * @throws \Exception
     */
    public function generation()
    {
        //从DB model层 取出数据缓存
        $videoModel = new Video();
        $cat_ids = [1,2,3,0];
        foreach ($cat_ids as $cat_id) {
            $condition = [];
            if (!empty($cat_id)){
                $condition['cat_id'] = $cat_id;
            }
            try{
                $data = $videoModel->getCache($condition);
            }catch (\Exception $e){
                Logger::getInstance()->log('未能获取到数据库原数据');
                $data = [];
            }
            
            if (empty($data)){
                continue;
            }

            foreach ($data as &$row) {
                $row['create_time'] = date('Ymd H:i:s');
                $row['duration'] = gmstrftime("%H:%M:%S",$row['duration']);
            }

            switch ($this->type){
                case 'file':
                    $res = file_put_contents($this->getCacheFileName($cat_id),json_encode($data));
                    break;
                case 'table':
                    if (!empty($data)){
                        $res = Cache::getInstance()->set($this->getCacheKey($cat_id,json_encode($data)));
                    }
                    break;
                case 'redis':
                    if (!empty($data)){
                        $res = Di::getInstance()->get("REDIS")->set($this->getCacheKey($cat_id),json_encode($data));
                    }
                    break;
                default:
                    throw new \Exception("请求不合法");
                    break;
            }

            if (empty($res)){
                Logger::getInstance()->log('数据缓存操作失败');
            }
        }
        
        
    }

    /**
     * 按文件缓存获取文件名
     * @param $cat_id
     * @return string
     */
    public function getCacheFileName($cat_id)
    {
        return EASYSWOOLE_ROOT."/Temp/cache/".$cat_id.".json";
    }

    /**
     * 按内存缓存获取缓存键名
     * @param $cat_id
     * @return string
     */
    public function getCacheKey($cat_id)
    {
        return "index_video_data_cat_id_".$cat_id;
    }

    public function getData($page = 1,$cat_id = 0,$size = 10)
    {
        //从缓存获取数据
        switch ($this->type){
            case 'file':
                $cacheData = file_get_contents($this->getCacheFileName($cat_id));
                break;
            case 'table':
                $cacheData = Cache::getInstance()->get($this->getCacheKey($cat_id));
                break;
            case 'redis':
                $cacheData = Di::getInstance()->get('REDIS')->get($this->getCacheKey($cat_id));
                break;
            default:
                throw new \Exception('非法请求');
        }
        if (empty($cacheData)){
            return [];
        }else{
            $cacheData = json_decode($cacheData,true);
            //按分页设置分割数据
            $total = count($cacheData);
            $result = array_slice($cacheData,($page-1)*$size,$size);
            return $result;
        }
    }
}